package com.longshihan.carservice.controller;

import com.github.pagehelper.PageInfo;
import com.longshihan.carservice.http.APIResponse;
import com.longshihan.carservice.model.LogDomain;
import com.longshihan.carservice.service.log.LogService;
import com.longshihan.carservice.utils.GsonUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author longhe
 * @time
 */
@Api("日志相关")
@Controller
@RequestMapping("/log")
public class LogController extends BaseController {


    @Autowired
    private LogService logService;


    @ApiOperation(value = "新增日志")
    @PostMapping(value = "/addLog")
    @ResponseBody
    public APIResponse saveLog(HttpServletRequest request,  HttpServletResponse response,
                                 @ApiParam(name = "action", value = "行为", required = true)
                                 @RequestParam(name = "action", required = true) String action,
                                 @ApiParam(name = "data", value = "数据", required = true)
                                 @RequestParam(name = "data", required = true) String data) {
        try{
            logService.addLog(action,data);
            return APIResponse.success();
        }catch (Exception e){
            String msg = "添加日志";
            return APIResponse.fail(e.getMessage());
        }
    }

    @ApiOperation(value = "所有日志")
    @PostMapping(value = "/getLogs")
    @ResponseBody
    public APIResponse getAllArticleByUserId(HttpServletRequest request,  HttpServletResponse response,
                                             @ApiParam(name = "pageNum", value = "页面数",defaultValue = "1") @RequestParam(name = "pageNum",defaultValue = "1") int pageNum,
                                             @ApiParam(name = "pagesize", value = "单次查询的个数", required = true,defaultValue = "20") @RequestParam(name = "pagesize", required = true,defaultValue = "20") int pagesize
    ) {
        try{
            if (pageNum==0){
                pageNum=1;
            }
            if (pagesize==0){
                pagesize=20;
            }
            PageInfo<LogDomain> logDomainPageInfo=logService.getLogs(pageNum,pagesize);
            if (logDomainPageInfo!=null){
                return APIResponse.success(GsonUtils.toJsonString(logDomainPageInfo));
            }else {
                return APIResponse.fail("获取不到日志信息");
            }

        }catch (Exception e){
            String msg = "查询日志失败";
            return APIResponse.fail(e.getMessage());
        }
    }
}
